<?php
class control extends base
{
	function control()
	{
		$this->base();
		$this->load_class('profile');
		$this->load_class('user');
	}
	function actionmanage()
	{
		$uid = $_SESSION['user_id'];
		$profiledata = $_ENV['profile']->profiledata($uid);
		$this->view->assign('profiledata',$profiledata);
		$this->view->display('profile.htm');
	}
	function actionupdate()
	{
		if(getgpc('do','P') =='profile')
		{
			$_POST = escape($_POST);
			$row = array();
			$row['truename'] = $_POST['truename'];
			$row['user_description'] = $_POST['user_description'];
			$row['birthyear'] = $_POST['birthyear'];
			$row['birthmonth'] = $_POST['birthmonth'];
			$row['birthday'] = $_POST['birthday'];
			$row['sex'] = $_POST['sex'];
			$row['marry'] = $_POST['marry'];		
			$result = $_ENV['profile']->update($row);		
			if($result)
			{
				$this->redirect('index.php?m=profile&a=manage',4000,'更新成功');
				exit();
			}
		}
		elseif(getgpc('do','P')=='contact')
		{
			$_POST = escape($_POST);
			$row = array();
			$row['website'] = $_POST['website'];
			$row['qq'] = $_POST['qq'];
			$result = $_ENV['profile']->update($row);
			if($result)
			{
				$this->redirect('index.php?m=profile&a=manage',4000,'更新成功');
				exit();
			}
		}
	}
	function actionupdateheadpic()
	{
		include(AM_ROOT.'lib/function_image.php');
		$allowfiletype = array('jpg','gif');
		$title = $_FILES['headpic']['name'];
		$fileext = strtolower(trim(substr(strchr($title,'.'),1)));
		if(!in_array($fileext,$allowfiletype))
		{
			$this->redirect('index.php?m=profile&a=manage',4000,'不支持该图片类型');
			exit();
		}
		$uid = abs(intval($_SESSION['user_id']));
		$uid ?'': exit('ACCESS DENIED');
		$uid = sprintf("%09d", $uid);
		$dir1 = substr($uid, 0, 3);
		$dir2 = substr($uid, 3, 2);
		$dir3 = substr($uid, 5, 2);
		$url = $dir1.'/'.$dir2.'/'.$dir3;
		mkdirs(AM_UPLOAD.'avatar/'.$url);
		$result = makethumb($_FILES['headpic']['tmp_name'],64,64,AM_UPLOAD.'avatar/'.$url.'/'.substr($uid, -2).'_avatar.gif');
		
		if($result)
		{
			$row = array();
			$_ENV['profile']->update($row);			
			$this->redirect('index.php?m=profile&a=manage',4000,'更新头像成功');
			exit();
		}
	}
	function actionview()
	{
		$this->load_class('comment');
		$uid = is_numeric(getgpc('userid','G'))?intval(getgpc('userid','G')):getgpc('user_id','S');
		if($uid)
		{
			$username = $_ENV['user']->get_username($uid);
			if($username)
			{
				$cpage = isset($_GET['cpage'])?intval($_GET['cpage']):1;				
				$cperpage  = 5;//每页评论数
				$cf = ($cpage-1)*$cperpage;//查询开始数
				$cnum = $_ENV['comment']->get_count_by_id($uid,3);//获得评论记录总数
				$commentdata = $_ENV['comment']->comment_list($uid,3,$cf,$cperpage);
				$commentmulti = $this->multi($cnum,$cperpage,$cpage,'index.php?m=profile&a=view&userid='.$uid,'cpage');
				$profiledata = $_ENV['profile']->profiledata($uid);
				$sexarray = array('1'=>'女','2'=>'男','0'=>'保密');
				$marryarr = array('1'=>'单身','2'=>'已成眷属','0'=>'保密');
				if($profiledata)
				{
					$sex = $sexarray[$profiledata['sex']];
					$marry = $marryarr[$profiledata['marry']];
				}
				else
				{
					$sex = '保密';
					$marry = '保密';
				}
				$this->view->assign('sex',$sex);
				$this->view->assign('marry',$marry);
				$this->view->assign('commentmulti',$commentmulti);
				$this->view->assign('commentdata',$commentdata);
				$this->view->assign('username',$username);
				$this->view->assign('uid',$uid);
				$this->view->assign('profiledata',$profiledata);
				$this->view->display('profile_view.htm');
			}
		}
	}
}
?>